package com.ui.home.view;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.base.BaseRefreshFragment;
import com.bean.SearchListBean;
import com.bean.WxArticleAuthorBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ddd.wandemo.ArticleDetailWebView;
import com.ddd.wandemo.R;
import com.ui.home.adapter.SearchResultRvAdapter;
import com.ui.home.adapter.WxAuthorAdapter;
import com.ui.home.presenter.WxArticleFragmentContract;
import com.ui.home.presenter.WxArticleFragmentPresenter;
import com.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeWxArticleFragment extends BaseRefreshFragment<WxArticleFragmentPresenter> implements WxArticleFragmentContract.View{


    private RecyclerView mRvArticle;
    private SwipeRefreshLayout mSrlWxArtirle;
    private RecyclerView mRvAuthor;
    private int page = 0;
    private WxAuthorAdapter mAuthorAdapter;
    private SearchResultRvAdapter mSearchResultRvAdapter;
    private List<SearchListBean.DatasBean> datas = new ArrayList<>();
    private int mAuthorId;

    @Override
    protected WxArticleFragmentPresenter initPresenter() {
        return new WxArticleFragmentPresenter(getActivity());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_wxarticle;
    }

    @Override
    protected void initView() {
        mRvArticle = view.findViewById(R.id.rv_wx_article);
        mRvAuthor = view.findViewById(R.id.rv_author);
        mSrlWxArtirle = view.findViewById(R.id.srl_wx_article);
        mSrlWxArtirle.setColorSchemeColors(mContext.getResources().getColor(R.color.colorAccent));

        mAuthorAdapter = new WxAuthorAdapter(getActivity());
        mRvAuthor.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvAuthor.setAdapter(mAuthorAdapter);

        mRvArticle.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSearchResultRvAdapter = new SearchResultRvAdapter(datas);
        mRvArticle.setAdapter(mSearchResultRvAdapter);
    }

    @Override
    protected void initEventAndData() {
        mAuthorAdapter.setOnItemClickListener(new WxAuthorAdapter.OnItemClickListener() {
            @Override
            public void itemClick(int id) {
                ToastUtil.showToast(id+"");
            }
        });

        mSearchResultRvAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getArticle(mAuthorId,++page);
            }
        },mRvArticle);

        mSrlWxArtirle.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                mPresenter.getArticle(mAuthorId,page);
            }
        });

        //结果item点击
        mSearchResultRvAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchListBean.DatasBean data = (SearchListBean.DatasBean) adapter.getData().get(position);
                Intent intent = new Intent(getActivity(), ArticleDetailWebView.class);
                intent.putExtra("link",data.getLink());
                intent.putExtra("title",data.getTitle());
                intent.putExtra("type",ArticleDetailWebView.TYPE_ARTICLE);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void loadData() {
        mPresenter.getAuthor();
    }

    @Override
    public void onGetAuthor(List<WxArticleAuthorBean> authorBean) {
        mAuthorId = authorBean.get(0).getId();
        mAuthorAdapter.setData(authorBean);
        mPresenter.getArticle(mAuthorId,page);
        mSrlWxArtirle.setRefreshing(false);
    }

    @Override
    public void onGetArticle(List<SearchListBean.DatasBean> datas) {
        mSearchResultRvAdapter.setNewData(datas);
        //首次进入不走loadmore回调
        mSearchResultRvAdapter.disableLoadMoreIfNotFullPage();
        mSrlWxArtirle.setRefreshing(false);
    }

    @Override
    public void onLoadArticle(List<SearchListBean.DatasBean> beans) {
        if (beans.size() == 0) {
            mSearchResultRvAdapter.loadMoreEnd();
        } else {
            mSearchResultRvAdapter.addData(beans);
            mSearchResultRvAdapter.loadMoreComplete();
        }
    }
}
